﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ModelViewSymbolNewParameterOverrideBase.h"
#include "IDocument.h"
#include "IGenericElement.h"
#include "IModelViewSymbolNew.h"
#include "IModelView.h"
#include "IElementParameters.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

#pragma region ParameterOverrideBase

UniIdentity ModelViewSymbolNewParameterOverrideBase::GetUniIdentity() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->GetUniIdentity() : UniIdentity::InvalidUid;
}

bool ModelViewSymbolNewParameterOverrideBase::ShouldOverrideIsModifiable() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->ShouldOverrideIsModifiable() : false;
}

bool ModelViewSymbolNewParameterOverrideBase::IsModifiable() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->IsModifiable() : false;
}

bool ModelViewSymbolNewParameterOverrideBase::ShouldOverrideIsUserVisible() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->ShouldOverrideIsUserVisible() : false;
}

bool ModelViewSymbolNewParameterOverrideBase::IsUserVisible() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->IsUserVisible() : false;
}

bool ModelViewSymbolNewParameterOverrideBase::ShouldOverrideGetName() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->ShouldOverrideGetName() : false;
}

std::wstring ModelViewSymbolNewParameterOverrideBase::GetName() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->GetName() : L"";
}

bool ModelViewSymbolNewParameterOverrideBase::ShouldOverrideGetValidator() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->ShouldOverrideGetValidator() : false;
}

const IParameterValidator* ModelViewSymbolNewParameterOverrideBase::GetValidator() const
{
    const IParameterOverride* pSectionViewParameterOverride = GetModelViewParameterOverride();
    return pSectionViewParameterOverride ? pSectionViewParameterOverride->GetValidator() : nullptr;
}

const IParameterOverride* ModelViewSymbolNewParameterOverrideBase::GetModelViewParameterOverride() const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"Invalid pGenericElement",L"GDMPLab",L"2024-03-30");

    const IDocument* pDoc = pGenericElement->GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"Invalid pDoc",L"GDMPLab",L"2024-03-30");

    const IModelViewSymbolNew* pSymbol = quick_cast<IModelViewSymbolNew>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSymbol, L"Invalid pSymbol",L"GDMPLab",L"2024-03-30");

    const IModelView* pModelView = quick_cast<IModelView>(pDoc->GetElement(pSymbol->GetModelViewId()));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"Invalid pModelView",L"GDMPLab",L"2024-03-30");

    const IElementParameters* pElementParams = pModelView->GetElementParameters();
    if (!pElementParams)
        return nullptr;

    UniIdentity uid = GetUidForParameterOverride();
    return pElementParams->GetIndividualParameterOverride(uid);
}

#pragma endregion ParameterOverrideBase